using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration.Claims;
using System.Collections.Generic;
using System.IO;

namespace g2rcp
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("53f21f4b-ae17-4f69-acff-656b838a7aba")]
    public class G2RCPEventReceiver : SPClaimProviderFeatureReceiver
    {
        public override string ClaimProviderAssembly
        {
            get {
                G2RCP.LogToULS("ClaimProviderAssembly: " + typeof(G2RCP).Assembly.FullName, Microsoft.SharePoint.Administration.TraceSeverity.Medium, Microsoft.SharePoint.Administration.EventSeverity.Information);
                return typeof(G2RCP).Assembly.FullName; }
        }

        public override string ClaimProviderDescription
        {
            get {
                G2RCP.LogToULS("ClaimProviderDescription: " + G2RCP._ProviderDisplayName, Microsoft.SharePoint.Administration.TraceSeverity.Medium, Microsoft.SharePoint.Administration.EventSeverity.Information);
                return G2RCP._ProviderDisplayName;
            }
        }

        public override string ClaimProviderDisplayName
        {
            get {
                G2RCP.LogToULS("ClaimProviderDisplayName: " + G2RCP._ProviderDisplayName, Microsoft.SharePoint.Administration.TraceSeverity.Medium, Microsoft.SharePoint.Administration.EventSeverity.Information);
                return G2RCP._ProviderDisplayName;
            }
        }

        public override string ClaimProviderType
        {
            get {
                G2RCP.LogToULS("ClaimProviderType: " + typeof(G2RCP).FullName, Microsoft.SharePoint.Administration.TraceSeverity.Medium, Microsoft.SharePoint.Administration.EventSeverity.Information);
                return typeof(G2RCP).FullName;
            }
        }

        private void ExecBaseFeatureActivated(Microsoft.SharePoint.SPFeatureReceiverProperties properties)
        {
            // Wrapper function for base FeatureActivated. 
            // Used because base keyword can lead to unverifiable code inside lambda expression
            base.FeatureActivated(properties);
        }

        private void RemovePersistedObject()
        {
            var PersistedObject = G2RCPConfig.GetFromConfigDB;
            if (PersistedObject != null)
                PersistedObject.Delete();
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            ExecBaseFeatureActivated(properties);
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //using (StreamWriter outfile = new StreamWriter(@"c:\temp\featurereceiver.txt", true))
                //{
                //    outfile.WriteLine(DateTime.Now.ToString() + " - FeatureUninstalling called");
                //}
                //base.RemoveClaimProvider(G2RCP._ProviderInternalName);
                this.RemovePersistedObject();
            });
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                //using (StreamWriter outfile = new StreamWriter(@"c:\temp\featurereceiver.txt", true))
                //{
                //    outfile.WriteLine(DateTime.Now.ToString() + " - FeatureDeactivating called");
                //}
                base.RemoveClaimProvider(G2RCP._ProviderInternalName);
                //var trust = G2RCP.GetSPTrustAssociatedWithCP(G2RCP._ProviderInternalName);
                //if (trust != null)
                //{
                //    trust.ClaimProviderName = null;
                //    trust.Update();
                //}
                this.RemovePersistedObject();

            });
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                this.RemovePersistedObject();
            });
        }

        public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, IDictionary<string, string> parameters)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                this.RemovePersistedObject();
            });
        }
    }
}
